package com.payment.ktb.activity.main1;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.utils.PhtotUtils;
import com.payment.ktb.utils.SharedPreferencesUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/8/26.
 */
public class IsVIPActivity extends BaseActivity {



    //版本比较：是否是4.4及以上版本
    static boolean mIsKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    File file = new File(Environment.getExternalStorageDirectory(), SharedPreferencesUtils.getString(ConstantsUser.phone) + "avatar.png");
    Activity thisActivity;
    int takePhotoRequestCode = 5;
    int pickPhotoRequestCode = 6;
    @BindView(R.id.iv_isvip_avatar)
    ImageView iv_isvip_avatar;
    @BindView(R.id.tv_isvip_username)
    TextView tv_isvip_username;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_isvip);
        ButterKnife.bind(this);
        initTitle("VIP会员");
        tv_isvip_username.setText(SharedPreferencesUtils.getString(SharedPreferencesUtils.getString(ConstantsUser.phone) + ConstantsUser.SHOPACCOUNT));
        thisActivity = this;
    }




    @OnClick({R.id.iv_isvip_grywx,R.id.iv_isvip_lmxyk,R.id.iv_isvip_avatar})
    public void clickEvent(View view) {

        switch (view.getId()) {

            //个人意外险
            case R.id.iv_isvip_grywx:
                startActivity(VIPInsuranceActivity.class);
                break;

            //联名信用卡
            case R.id.iv_isvip_lmxyk:
                startActivity(VIPCreditCardActivity.class);
                break;

            //个人头像
            case R.id.iv_isvip_avatar:
                setAvatar();
                break;


        }



    }

    /**
     *
     */
    private void setAvatar() {


        final String[] stringItems = {"拍照", "从相册中选择"};
        final ActionSheetDialog dialog = new ActionSheetDialog(mContext, stringItems, null);
        dialog.isTitleShow(false).show();
        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position == 0) {
                    PhtotUtils.takePhoto(thisActivity, file, takePhotoRequestCode);
                } else if (position == 1){
                    PhtotUtils.pickPhoto(thisActivity,pickPhotoRequestCode);
                }
                dialog.dismiss();
            }
        });
    }





    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if (requestCode == takePhotoRequestCode && resultCode == RESULT_OK) {

            // 对拍照之后的图片进行压缩。
            final Bitmap bitmap = PhtotUtils.compressBySize(file.getPath(), 500, 500);
            Bitmap b = PhtotUtils.compressImage(bitmap);
            ByteArrayOutputStream byos = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG,100,byos);
            try {
                FileOutputStream f = new FileOutputStream(file);
                try {
                    f.write(byos.toByteArray());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }


        } else if (requestCode == pickPhotoRequestCode && resultCode == RESULT_OK) {
            String pickPhotoPath = null;
            if (mIsKitKat) {
                pickPhotoPath = PhtotUtils.getPickPhotoPathAfterKitKat(mContext, data.getData());
            } else {
                pickPhotoPath = PhtotUtils.handleUri(this,data);
            }
            Bitmap bitmap = PhtotUtils.compressBySize(pickPhotoPath, 500, 500);
            Bitmap b = PhtotUtils.compressImage(bitmap);
            ByteArrayOutputStream byos = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG,100,byos);
            try {
                FileOutputStream f = new FileOutputStream(file);
                try {
                    f.write(byos.toByteArray());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (file != null && file.exists()) {
            iv_isvip_avatar.setImageBitmap(BitmapFactory.decodeFile(file.getPath()));
        }
    }
}
